package com.aftersale.demo.utils;

import cn.hutool.core.date.DateTime;
import com.aftersale.demo.service.LogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @author 空
 * @date 2023/11/23 21:01
 * @desc 定时删除数据库中日志数据
 */
@Component
@Slf4j
public class SchedulingTask {
    @Autowired
    LogService logMapper;

    @Async("task")
    @Scheduled(cron="0 00 12 ? * Mon")
    public void LogDelete(){
        //获取当前时间七天前的时间并按固定格式修改
        String timeBeforeSeven=LocalDateTime.now().
                minusDays(7).toString()
                .substring(0,19).replace("T"," ");
        QueryWrapper logQueryWrapper=new QueryWrapper();
        logQueryWrapper.le("time",timeBeforeSeven);
        logMapper.remove(logQueryWrapper);
        log.info("系统定时删除日志已执行");
    }

}
